    //
//  BetViewController.m
//  BetsAndBrains
//
//  Created by Augustine, Eric M (augus011) on 4/9/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "BetViewController.h"
#import "ResultViewController.h"
#import "Game.h"
#import "Player.h"
#import "Question.h"
#import "Bet.h"
#import "Guess.h"
#import "ComputerPlayer.h"

@implementation BetViewController

@synthesize resultView;
@synthesize question;
@synthesize tf1;
@synthesize tf2;
@synthesize tf3;
@synthesize tf4;
@synthesize tf5;
@synthesize tf6;
@synthesize l1;
@synthesize l2;
@synthesize l3;
@synthesize l4;
@synthesize l5;
@synthesize l6;
@synthesize br1;
@synthesize br2;
@synthesize br3;
@synthesize br4;
@synthesize br5;
@synthesize br6;
@synthesize game;

-(IBAction)placeBets{
	Player* p1 = [self.game.players objectAtIndex:0];
	NSInteger chipsBet = 0;
	NSInteger totalPlayerChips = p1.permChips + p1.gameChips;
	//Check all of the text fields
	if(tf1.text != @""){
		if([self checkText:tf1.text]){
			chipsBet += [tf1.text intValue];
			[p1 placeBet:0 :[tf1.text intValue]];
		}
	}
	
	if(tf2.text != @""){
		if([self checkText:tf2.text]){
			chipsBet += [tf2.text intValue];
			[p1 placeBet:1 :[tf2.text intValue]];
		}
	}
	
	if(tf3.text != @""){
		if([self checkText:tf3.text]){
			chipsBet += [tf3.text intValue];
			[p1 placeBet:2 :[tf3.text intValue]];
		}
	}
	
	if(tf4.text != @""){
		if([self checkText:tf4.text]){
			chipsBet += [tf4.text intValue];
			[p1 placeBet:3 :[tf4.text intValue]];
		}
	}
	
	if(tf5.text != @""){
		if([self checkText:tf5.text]){
			chipsBet += [tf5.text intValue];
			[p1 placeBet:4 :[tf5.text intValue]];
		}
	}
	
	if(tf6.text != @""){
		if([self checkText:tf6.text]){
			chipsBet += [tf6.text intValue];
			[p1 placeBet:5 :[tf6.text intValue]];
		}
	}
	
	if(chipsBet <= totalPlayerChips){
		for(NSInteger i = 1; i < self.game.players.count; i++){
			[self.game resetChipBets];
			ComputerPlayer* comp = [self.game.players objectAtIndex:i];
			[comp chooseBet];
		}
		[self.game winnings];
		resultView.game = self.game;
		[self presentModalViewController:resultView animated: TRUE];
	}else{
		//Some prompt or something
		[self.game clearBets];
	}
}

-(BOOL)checkText:(NSString*)text{
	NSError* error = NULL;
	NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"^[0-9]+$" 
																		   options:NSRegularExpressionCaseInsensitive 
																			 error:&error];
	NSInteger matches = [regex numberOfMatchesInString:text
										  options:NSMatchingWithoutAnchoringBounds 
											range:NSMakeRange(0,text.length)];
	if(matches > 0){
		return TRUE;
	}
	return FALSE;
}

 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[self.question setText:game.currentQuestion.text];
	[self.l1 setText:[NSString stringWithFormat:@"%d",[[game.guesses objectAtIndex:0] guess]]];
	[self.l2 setText:[NSString stringWithFormat:@"%d",[[game.guesses objectAtIndex:1] guess]]];
	[self.l3 setText:[NSString stringWithFormat:@"%d",[[game.guesses objectAtIndex:2] guess]]];
	[self.l4 setText:[NSString stringWithFormat:@"%d",[[game.guesses objectAtIndex:3] guess]]];
	[self.l5 setText:[NSString stringWithFormat:@"%d",[[game.guesses objectAtIndex:4] guess]]];
	[self.l6 setText:[NSString stringWithFormat:@"%d",[[game.guesses objectAtIndex:5] guess]]];
	[self.br1 setText:[NSString stringWithFormat:@"%.1f", 2.0]];
	[self.br2 setText:[NSString stringWithFormat:@"%.1f", 2.0]];
	[self.br3 setText:[NSString stringWithFormat:@"%.1f", 2.0]];
	[self.br4 setText:[NSString stringWithFormat:@"%.1f", 2.0]];
	[self.br5 setText:[NSString stringWithFormat:@"%.1f", 2.0]];
	[self.br6 setText:[NSString stringWithFormat:@"%.1f", 2.0]];
	
}


-(IBAction) dismissKeyboardBackground:(id)sender{
	[sender resignFirstResponder];
}

-(IBAction) tapBackground:(id)sender{
	[tf1 resignFirstResponder];
	[tf2 resignFirstResponder];
	[tf3 resignFirstResponder];
	[tf4 resignFirstResponder];
	[tf5 resignFirstResponder];
	[tf6 resignFirstResponder];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
	
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil
	self.resultView = nil;
	self.question = nil;
	self.tf1 = nil;
	self.tf2 = nil;
	self.tf3 = nil;
	self.tf4 = nil;
	self.tf5 = nil;
	self.tf6 = nil;
	self.l1 = nil;
	self.l2 = nil;
	self.l3 = nil;
	self.l4 = nil;
	self.l5 = nil;
	self.l6 = nil;
	self.br1 = nil;
	self.br2 = nil;
	self.br3 = nil;
	self.br4 = nil;
	self.br5 = nil;
	self.br6 = nil;
	self.game = nil;
}


- (void)dealloc {
    [super dealloc];
	[resultView autorelease];
	[question autorelease];
	[tf1 autorelease];
	[tf2 autorelease];
	[tf3 autorelease];
	[tf4 autorelease];
	[tf5 autorelease];
	[tf6 autorelease];
	[l1 autorelease];
	[l2 autorelease];
	[l3 autorelease];
	[l4 autorelease];
	[l5 autorelease];
	[l6 autorelease];
	[br1 autorelease];
	[br2 autorelease];
	[br3 autorelease];
	[br4 autorelease];
	[br5 autorelease];
	[br6 autorelease];
	[game autorelease];
}


@end
